#pragma once

#include <interface/interface.h>
#include <interface/log_interface.h>
#include <interface/aot_inet_interface.h>
#include <interface/aot_im_interface.h>
#include <utility/ini_op.h>
#include <string>
#include <sys/sys_asy.h>
#include <sys/sys_thread.h>

struct inet_cfg
{
	int use_proxy;
	int proxy_type;
	std::string proxy_host_name;
	int proxy_port;
	std::string user;
	std::string pwd;
	int user_spec_ims;
	std::string spec_ims_ip;
	int spec_ims_port;
	int spec_ims_type;

	std::string ims_ip;
	int ims_port;
	int ims_type;

	inet_cfg()
	{
		ims_port = 0;
		ims_type = 0;
		use_proxy = 0;
		user_spec_ims = 0;
	}

	bool read_cfg();
};

class aot_inet_syn_client : public xy::thread_base
{
public:
	aot_inet_syn_client();
	~aot_inet_syn_client();
public:
	bool init();
	bool connect();
	bool send(aot::inet::ipacket* pkt);
	void close();
	void report_reconnect();
protected:
	int svc();
private:
	bool get_self_info();
	aot::im::iim_packet* build_im_packet(aot::inet::ipacket* inet_pkt);
	bool send_i(aot::inet::ipacket* pkt);
	int recv(aot::inet::ipacket** pkt, const aot::inet::inet_time_t* tm_out = NULL);
	int recv_i(aot::inet::ipacket** pkt, const aot::inet::inet_time_t* tm_out = NULL);
public:
	aot::tt::iobj_auto_ptr<aot::inet::itcp_syn_channel> tcp_syn_channel_;
	aot::tt::utility::ini_op ini_;
	xy::thread_mutex connect_mutex_;
	xy::thread_condition connect_cdt_;
	bool need_report_reconnect_;
	bool is_connect_;
	std::string serv_ip_;
	int ims_port_;
	std::string my_public_ip_;
	std::string my_inner_ip_;
	int my_inner_port_;

	inet_cfg inet_cfg_;
};